package end.ser.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import end.bean.shiro.Manager;
import end.bean.shiro.Role;
import end.dao.ManagerDAO;
import end.ser.ManagerSER;
import end.ser.RoleSER;

/**
 * 
 * ManagerSER.java 2016-6-25
 * 
 * Copyright zhaocj Inc. All rights reserved. Love Me Like Love Justin Bieber
 */
@Service
@Transactional
public class ManagerSERImp<T> extends BaseSERImp<T> implements ManagerSER<T> {
	@Autowired
	private ManagerDAO<T> mapperDAO;

	public ManagerDAO<T> getMapperDAO() {
		return mapperDAO;
	}

	public void setMapperDAO(ManagerDAO<T> mapperDAO) {
		this.mapperDAO = mapperDAO;
	}

	@Autowired
	private RoleSER<Role> roleSER;

	public RoleSER<Role> getRoleSER() {
		return roleSER;
	}

	public void setRoleSER(RoleSER<Role> roleSER) {
		this.roleSER = roleSER;
	}

	@SuppressWarnings("unchecked")
	@Override
	public T selectByPrimaryKey(Object key) throws Exception {
		T t = getMapperDAO().selectByPrimaryKey(key);
		Manager manager = (Manager) t;
		if (manager != null) {
			List<Role> roles = roleSER.queryRolesByManagerId(manager.getId());
			manager.setRoles(roles);
		}
		return (T) manager;
	}

	@Override
	public Manager queryManagerByOption(String nickName, String password) {
		return mapperDAO.queryManagerByOption(null, nickName, password);
	}

	@Override
	public Integer modifyPass(Object key) {
		return mapperDAO.modifyPass(key);
	}

	@Override
	public List<Manager> queryManagersByPage(Integer page, Integer rows,
			String sort, String order) {
		return mapperDAO.queryManagersByPage(page, rows, sort, order);
	}

	@Override
	public Integer count() {
		return mapperDAO.count();
	}

	@Override
	public boolean isManagerCodeAlreadyUsed(Manager manager) {
		if (mapperDAO.isManagerCodeAlreadyUsed(manager) > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public boolean modifyManager(Manager manager) {
		return mapperDAO.modifyManager(manager);
	}
}
